//
//  MYGTopicTopCommentsTableViewCell.swift
//  MYDoctor
//
//  Created by 王振明 on 2024/4/3.
//

import UIKit

class MYGTopicFartherCommentsView: UIView {
    var tapComonCallBack:((MYGTopicCommentModel,Int)->())?
    var currentSection = 0
    var model = MYGTopicCommentModel()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
//    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
//        super.init(style: style, reuseIdentifier: reuseIdentifier)
//       
//    }
    
    func setupUI() {
        addSubview(headerImageBV)
        headerImageBV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview().offset(15)
            make.size.equalTo(CGSize(width: 40, height:40))
        }
        
        addSubview(nameLabel)
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headerImageBV.snp.right).offset(8)
            make.top.equalTo(headerImageBV.snp.top)
            make.height.equalTo(18.5)
        }
        
        addSubview(contentLab)
        contentLab.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.left)
            make.top.equalTo(nameLabel.snp.bottom).offset(5)
            make.right.equalToSuperview().offset(-70)
        }

        addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(contentLab.snp.left)
            make.top.equalTo(contentLab.snp.bottom).offset(7)
            make.height.equalTo(11)
        }
        
        addSubview(commentBtn)
        commentBtn.snp.makeConstraints { make in
            make.left.equalTo(timeLabel.snp.right).offset(3.5)
            make.centerY.equalTo(timeLabel)
            make.size.equalTo(CGSize(width: 20, height: 30))
        }
        addSubview(zanLabel)
        zanLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-12)
            make.top.equalToSuperview().offset(40)
            make.height.equalTo(16.5)
        }
        addSubview(zanBtn)
        zanBtn.snp.makeConstraints { make in
            make.right.equalTo(zanLabel.snp.left)
            make.centerY.equalTo(zanLabel)
            make.size.equalTo(CGSize(width: 30, height: 30))
        }
        
        addSubview(line)
        line.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.left)
            make.right.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private lazy var headerImageBV: UIImageView = {
        let cont = UIImageView()
        cont.layer.cornerRadius = 20
        cont.layer.masksToBounds = true
        cont.contentMode = .scaleAspectFill
        return cont
    }()
    private lazy var nameLabel: UILabel = {
        let cont = UILabel()
        cont.text = "武艺星灿"
        cont.font = .regular(13)
        cont.textColor = .rgba(102, 102, 102)
        return cont
    }()
    private lazy var contentLab: UILabel = {
        let cont = UILabel()
        cont.text = "武艺星灿武艺星灿武艺星灿武艺星灿武艺星灿武艺星灿"
        cont.font = .regular(14)
        cont.textColor = .rgba(51, 51, 51)
        cont.numberOfLines = 0
        return cont
    }()
    private lazy var timeLabel: UILabel = {
        let cont = UILabel()
        cont.text = "2023-01-01"
        cont.font = .regular(8)
        cont.textColor = .rgba(204, 204, 204)
        return cont
    }()
    private lazy var commentBtn: UIButton = {
        let cont = UIButton()
        cont.addTarget(self, action: #selector(tapComment), for: .touchUpInside)

        cont.setImage(UIImage(named: "MYG_home_comment"), for: .normal)
        return cont
    }()
    
    var line: UIView = {
        let control = UIView()
        control.backgroundColor = UIColor.init(hexString: "#eeeeee")
        return control
    }()
    private lazy var zanBtn: UIButton = {
        let cont = UIButton()
        cont.setImage(UIImage(named: "MYG_home_zan"), for: .normal)
        cont.setImage(UIImage(named: "MYG_cloudDisk_liked"), for: .selected)
        cont.addTarget(self, action: #selector(tapLike), for: .touchUpInside)

        return cont
    }()
    private lazy var zanLabel: UILabel = {
        let cont = UILabel()
        cont.text = "259"
        cont.font = .regular(12)
        cont.textColor = .rgba(128, 128, 128)
        return cont
    }()

    func bindData(_ data:MYGTopicCommentModel) {
        self.model = data
        self.zanLabel.text = "\(data.like_num)"
        self.nameLabel.text = data.doctor.name
        self.headerImageBV.sd_setImage(with: URL(string: data.doctor.avatar), placeholderImage: UIImage.init(named: "MYG_defaultAvatar_doctor"), context: nil)
        self.timeLabel.text = data.created_at
        self.zanBtn.isSelected = data.is_like != 0
        self.contentLab.text = data.comment

    }
    
    @objc func tapComment() {
        if let c = tapComonCallBack {
            c(self.model,self.currentSection)
        }
    }
    
    @objc func tapLike() {
        var likeStatus = "1"
        if self.model.is_like != 0 {
            //取消点赞
            likeStatus = "0"
            self.model.is_like = 0
            self.zanBtn.isSelected = false
            self.model.like_num = max(0, self.model.like_num - 1)
        }else {
            likeStatus = "1"
            self.model.is_like = 1
            self.zanBtn.isSelected = true
            self.model.like_num = max(0, self.model.like_num + 1)
        }
        self.zanLabel.text = "\(self.model.like_num)"

        MYGNetwork<MYGMeAPI, MYGEmptyModel>.request(target: .commentLike(param: [
            "id":self.model.id,"like_status":likeStatus
           ])) { item in
               
       } failed: { _, error in
           MMYGHUD.showToast(text: error)
       }
    }
}
